package com.lamda.zhekou.dao.impl;

import com.lamda.zhekou.dao.UserVpsDao;
import com.lamda.zhekou.model.UserVps;
import com.lamda.zhekou.model.VPS;
import com.lamda.zhekou.model.VpsBuyRecord;
import org.springframework.stereotype.Repository;

import javax.persistence.NoResultException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by MJiao on 15/8/16.
 */
@Repository
public class UserVpsDaoImpl extends GenericDaoImpl<UserVps> implements UserVpsDao {


    @Override
    public List<UserVps> findVPSRecordListByUserId(Long userId) {
        String qlString = "select entity from UserVps entity  where entity.user.id = :userId";

        return this.entityManager.createQuery(qlString).setParameter("userId", userId).getResultList();
    }

    @Override
    public synchronized UserVps findUserVpsByUserIdAndVpsId(Long userId, Long vpsId) {
        String qlString = "select e from UserVps e where e.user.id=:userId  and e.vps.id=:vpsId";
        try {
            return (UserVps) this.entityManager.createQuery(qlString).setParameter("userId", userId).setParameter("vpsId", vpsId).getSingleResult();
        } catch (NoResultException e) {
//  Do nothing
        }
        return null;
    }

    @Override
    public List<UserVps> getUserVpsListBy(Set<VPS> vpsSet) {
        String qlString = "select e from UserVps e where e.vps.id in :vpsIdList";
        List<Long> ids = new ArrayList<>();
        for (VPS vps : vpsSet) {
            ids.add(vps.getId());
        }
        return this.entityManager.createQuery(qlString).setParameter("vpsIdList", ids).getResultList();

    }

    @Override
    public List<UserVps> findUserVpsListByUserId(Long userId) {
        String qlString = "select e from UserVps e where e.user.id=:userId";
        return this.entityManager.createQuery(qlString).setParameter("userId", userId).getResultList();
    }

    @Override
    public List<UserVps> getLeftDaysVpsList(int gtDay, int ltDay) {
        //86400000 为1天的毫秒数
        //86400000 = 1000*3600*24
        long gtDayTime = 86400000l * gtDay;
        long ltDayTime = 86400000l * ltDay;

        Timestamp gtTimestamp = new Timestamp(System.currentTimeMillis() + gtDayTime);
        Timestamp ltTimestamp = new Timestamp(System.currentTimeMillis() + ltDayTime);

        String qlString = "select e from UserVps e where e.expireTime  < :ltDayTime and e.expireTime  > :gtDayTime";
        return this.entityManager.createQuery(qlString)
                .setParameter("ltDayTime", ltTimestamp)
                .setParameter("gtDayTime", gtTimestamp).getResultList();


    }
    @Override
    public List<UserVps> getExpiredVpsList() {

        Timestamp gtTimestamp = new Timestamp(System.currentTimeMillis());

        String qlString = "select e from UserVps e where e.vps.status='Running' and e.expireTime  < :gtDayTime";
        return this.entityManager.createQuery(qlString).setParameter("gtDayTime", gtTimestamp).getResultList();

    }

}
